package com.hydl.power.controller;


import com.github.pagehelper.PageInfo;
import com.hydl.power.aop.annotation.LogAnnotation;
import com.hydl.power.pojo.*;
import com.hydl.power.service.*;
import com.hydl.power.utils.IdUtil;
import com.hydl.power.utils.MD5Util;
import com.hydl.power.utils.Resp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.rmi.MarshalledObject;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Api(value = "AppController",tags = "App接口")
@RequestMapping("/app")
@RestController
@CrossOrigin
public class AppController {

    @Autowired
    private PowerDataService powerDataService;
    @Autowired
    private OrderDeliveryService orderDeliveryService;

    @Autowired
    private SysUserService userService;

    @Autowired
    private AppService appService;

    @Autowired
    private PushService pushService;

    @Autowired
    private TemperatureDeviceService temperatureDeviceService;

    @LogAnnotation(module = "App接口",description = "登录")
    @PostMapping("/login")
    @ApiOperation(value = "登录", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userName", value = "用户名", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "password", value = "密码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "clientId", value = "clientId",  dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "brand", value = "brand", dataType = "String"),
    })
    public Resp login(String userName, String password,String clientId,String brand) {
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
            return Resp.fail("不能为空");
        }
        SysUser user = userService.getUser(userName);
        if (user == null || !user.getPassword().equals(MD5Util.crypt(password))) {
            return Resp.fail("密码错误");
        }
        if (org.apache.commons.lang3.StringUtils.isNotBlank(clientId)){
            Push pushByClientId = pushService.getPushByClientId(clientId);
            if (pushByClientId==null){
                Push push=new Push();
                push.setName(userName);
                push.setClientId(clientId);
                push.setCreateTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
                if (user.getProjectId()!=null){
                    push.setProjectId(user.getProjectId().toString());
                }
                push.setStatus(user.getStatus());
                push.setBrand(brand);
                pushService.addPush(push);
            }
        }
        userService.updateLoginTime(user.getUsername());
        return Resp.success(user);
    }

    //@LogAnnotation(module = "App接口",description = "修改密码")
    @PostMapping("/updatePassword")
    @ApiOperation(value = "修改密码", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "password", value = "旧密码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "password1", value = "新密码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "password2", value = "重复密码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "token", value = "token", required = true, dataType = "String"),
    })
    public Resp updateAccount(String password, String password1, String password2,String token) {
        if (StringUtils.isEmpty(password) || StringUtils.isEmpty(password1) || StringUtils.isEmpty(password2) || !password1.equals(password2)) {
            return Resp.fail("修改失败");
        }
        SysUser  user=userService.getUserByToken(token);
        if (!user.getPassword().equals(MD5Util.crypt(password))) {
            return Resp.fail("旧密码不正确");
        }
        int i = userService.updatePassword(user.getId(), MD5Util.crypt(password1));
        return Resp.success("修改成功");
    }


    //@LogAnnotation(module = "App接口",description = "app首页")
    @PostMapping("getHomePage")
    @ApiOperation(value = "app首页", produces = "application/json")
    @ApiImplicitParams({
     @ApiImplicitParam(paramType = "query", name = "token", value = "token", dataType = "String",defaultValue="70N9D9nrpn"),
    })
    public Resp getHomePage(String token) throws ParseException {
        Map homePage = appService.getHomePage(token);
        return Resp.success(homePage);
    }

    //@LogAnnotation(module = "App接口",description = "查询待处理工单列表")
    @PostMapping("getPendingOrder")
    @ApiOperation(value = "待处理工单列表", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "token", value = "token", dataType = "String",defaultValue="70N9D9nrpn"),
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页数", dataType = "Integer",defaultValue="1"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "条数",dataType = "Integer",defaultValue="3"),
    })
    public Resp getPendingOrder(Integer pageNum,Integer pageSize,String token){
        PageInfo<WorkOrder> list= appService.getPendingOrder(token,pageNum,pageSize);
        return Resp.success(list);
    }

    //@LogAnnotation(module = "App接口",description = "查询处理中工单列表")
    @PostMapping("getBeingProcessedOrder")
    @ApiOperation(value = "处理中工单列表", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "token", value = "token", dataType = "String",defaultValue="70N9D9nrpn"),
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页数", dataType = "Integer",defaultValue="1"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "条数",dataType = "Integer",defaultValue="3"),
    })
    public Resp getBeingProcessedOrder(Integer pageNum,Integer pageSize,String token){
        PageInfo<WorkOrder> list= appService.getBeingProcessedOrder(token,pageNum,pageSize);
        return Resp.success(list);
    }

    //@LogAnnotation(module = "App接口",description = "查询已完成工单列表")
    @PostMapping("getGoodOrder")
    @ApiOperation(value = "已完成工单列表", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "token", value = "token", dataType = "String",defaultValue="70N9D9nrpn"),
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页数", dataType = "Integer",defaultValue="1"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "条数",dataType = "Integer",defaultValue="3"),
    })
    public Resp getGoodOrder(Integer pageNum,Integer pageSize,String token){
        PageInfo<WorkOrder> list= appService.getGoodOrder(token,pageNum,pageSize);
        return Resp.success(list);
    }


   // @LogAnnotation(module = "App接口",description = "忽略和完成工单")
    @PostMapping("updateOrderStatusById")
    @ApiOperation(value = "忽略和完成工单", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id", dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "orderStatus", value = "4 忽略工单 5 完成", dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "dispose", value = "处理结果", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "illustrate", value = "综合分析", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "reason", value = "故障原因", dataType = "String"),
    })
    public Resp updateOrderStatusById(Integer id,Integer orderStatus,String dispose,String illustrate,String reason) {
        int i = appService.updateOrderStatusById(id,orderStatus,dispose,illustrate,reason);
        if (i == 1){
            return Resp.success("修改成功");
        }
        return Resp.success("修改失败");
    }


    //@LogAnnotation(module = "App接口",description = "工单查询")
    @ApiOperation("工单查询")
    @PostMapping("/getOrderDelivery")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页数", dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "条数",dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "begin", value = "开始故障时间",dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "end", value = "结束故障时间",dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "orderStatus", value = "工单状态  1待派送 2待处理(派送工单) 3处理中 4 忽略工单(以忽略) 5 以结束",dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "maintainerId", value = "运维员Id", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "areaId", value = "区域id",dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "单位id",dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "roomId", value = "配电房id",dataType = "String"),
    })
    public Resp<PageInfo<OrderDelivery>> getOrderDelivery(Integer pageNum,Integer pageSize,String begin,String end,Integer orderStatus,Integer maintainerId,String areaId,String projectId,String roomId){
        PageInfo<OrderDelivery> orderDeliveryPageInfo = orderDeliveryService.getOrderDelivery(pageNum, pageSize, IdUtil.str2Long(begin), IdUtil.str2Long(end), orderStatus,maintainerId, areaId, projectId, roomId);
        return Resp.success(orderDeliveryPageInfo);
    }


    //@LogAnnotation(module = "App接口",description = "事件记录")
    @PostMapping("getIncidentRecord")
    @ApiOperation(value = "事件记录", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页数", dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "条数",dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "token", value = "token",dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "timeStatus", value = "1今日 2昨日 3本周 4本月", dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "faultTypes", value = "0全部告警 1越限告警 2变位告警 3电量反转 4时间反转 5数据异常 6 通信状态告警", dataType = "Integer"),
    })
    public Resp getIncidentRecord(Integer pageNum,Integer pageSize,String token,Integer timeStatus,Integer faultTypes) {
        PageInfo<WorkOrder> orderDeliveryPageInfo= appService.getIncidentRecord(pageNum,pageSize,token,timeStatus,faultTypes);
        return Resp.success(orderDeliveryPageInfo);
    }


    //@LogAnnotation(module = "App接口",description = "添加告警信息")
    @PostMapping("addOrderDelivery")
    @ApiOperation(value = "添加告警信息", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "deviceId", value = "设备id", dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "rank", value = " 处理级别 1 一般 2紧急 3严重", dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "faultTypes", value = " 故障类型(告警类型) 1越限告警 2变位告警 3电量反转 4时间反转 5数据异常 6 通信状态告警", dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "fault", value = "故障内容", dataType = "String"),
    })
    public Resp addOrderDelivery(String deviceId,Integer rank,Integer faultTypes,String fault) {
        int i = appService.addOrderDelivery(deviceId, rank, faultTypes, fault);
        if(i==1){
            return Resp.success("添加成功");
        }
        return Resp.success("添加失败",null,null);

    }


    //@LogAnnotation(module = "App接口",description = "获取城市列表")
    @PostMapping("getCity")
    @ApiOperation(value = "获取城市列表", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "token", value = "token", dataType = "String"),
    })
    public Resp getCity(String token) {
        List<City> list= appService.getCity(token);
        return Resp.success(list);
    }


    //@LogAnnotation(module = "App接口",description = "获取项目列表")
    @PostMapping("getProject")
    @ApiOperation(value = "获取项目列表", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "token", value = "token", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "cityId", value = "城市cityId", dataType = "String"),
    })
    public Resp getProject(String token,String cityId) {
        List<Project> list= appService.getProjectByCityId(token,cityId);
        return Resp.success(list);
    }


    //@LogAnnotation(module = "App接口",description = "获取配电房列表")
    @PostMapping("getPowerRoom")
    @ApiOperation(value = "获取配电房列表", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "token", value = "token", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id", dataType = "String"),
    })
    public Resp getPowerRoom(String token,String projectId) {
        List<PowerRoom> list= appService.getPowerRoomByProjectId(token,projectId);
        return Resp.success(list);
    }


    //@LogAnnotation(module = "App接口",description = "获取设备列表")
    @PostMapping("getDevice")
    @ApiOperation(value = "获取设备列表", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "token", value = "token", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "powerRoomId", value = "配电房id", dataType = "String"),
    })
    public Resp getDevice(String token,String powerRoomId) {
        List<Device> list= appService.getDeviceByPowerRoomId(token,powerRoomId);
        return Resp.success(list);
    }



    //@LogAnnotation(module = "App接口",description = "查询区域/单位/配电房/设备")
    @PostMapping("/getOrderDeliveryMenu")
    @ApiOperation(value = "区域/单位/配电房/设备", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "token", value = "token",defaultValue = "2O8QDw00Y1", dataType = "String"),
    })
    public Resp<LinkedHashSet<OrderDeliveryMenu>> getAllByPId(String token) {
        List<OrderDeliveryMenu> list=appService.getDevice(token);
        return Resp.success(list);
    }


    // @LogAnnotation(module = "App接口",description = "获取实时数据")
    @PostMapping("GetPowerData")
    @ApiOperation(value = "获取实时数据", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "navId", value = "设备id",defaultValue = "26916968", dataType = "String"),
    })
    public Resp GetPowerData(String navId) {
        Powerdata powerdata=appService.getPowerDataByDeviceId(navId);
        return Resp.success(powerdata);
    }

    //@LogAnnotation(module = "App接口",description = "确认收到工单")
    @PostMapping("affirmOrderStatusById")
    @ApiOperation(value = "确认收到工单", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "设备id",defaultValue = "1", dataType = "Integer"),
    })
    public Resp affirmOrderStatusById(Integer id) {
        int i = appService.affirmOrderStatusById(id);
        return Resp.success();
    }


    //@LogAnnotation(module = "App接口",description = "测温设备列表")
    @PostMapping("getTemperatureDeviceListByToken")
    @ApiOperation(value = "测温设备列表", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "token", value = "token",defaultValue = "", dataType = "String"),
    })
    public Resp getTemperatureDeviceListByToken(String  token) {
        List<TemperatureDevice> list = appService.getTemperatureDeviceListByToken(token);
        return Resp.success(list);
    }


    //@LogAnnotation(module = "App接口",description = "配件")
    @GetMapping("getParts")
    @ApiOperation(value = "配件", produces = "application/json")

    public Resp getParts() {
        List<Parts> list= appService.getParts();
        return Resp.success(list);
    }





}
